package com.woniuxy.exchangeType.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.woniuxy.utils.Utils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 在Direct模型下，队列与交换机的绑定，不能是任意绑定了，而是要指定一个RoutingKey（路由key）
 * 消息的发送方在向Exchange发送消息时，也必须指定消息的routing key
 */
public class DirectDeclare {

    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection = Utils.getConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //创建路由交换机
        channel.exchangeDeclare("directExchange", BuiltinExchangeType.DIRECT);
        //创建队列  durable表示是否持久化
        channel.queueDeclare("directQueue1",false,false,false,null);
        channel.queueDeclare("directQueue2",false,false,false,null);
        //交换机绑定队列
        channel.queueBind("directQueue1","directExchange","key1");
        channel.queueBind("directQueue2","directExchange","key2");
        //关闭资源
        channel.close();
        connection.close();

    }
}
